package com.websql.yousql.config.sqlGetter;

import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.executor.statement.StatementHandler;
import org.apache.ibatis.plugin.Interceptor;
import org.apache.ibatis.plugin.Intercepts;
import org.apache.ibatis.plugin.Invocation;
import org.apache.ibatis.plugin.Signature;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

@Component
@Intercepts({
        @Signature(type = StatementHandler.class,method = "prepare",args = {java.sql.Connection.class,Integer.class})
})
@Slf4j
public class SqlIntercept implements Interceptor {

    public static Map<String,Object> sqlAndParam = new HashMap<>();

    @Override
    public Object intercept(Invocation invocation) throws Throwable {

        StatementHandler statementHandler = (StatementHandler) invocation.getTarget();
        String sql = statementHandler.getBoundSql().getSql();
        Object parameterObject = statementHandler.getParameterHandler().getParameterObject();
        sqlAndParam.put("sql",sql);
        sqlAndParam.put("param",parameterObject);
 //       log.info("\n sql:{} \n param:{}",sql,parameterObject);
        return invocation.proceed();

    }
}
